$import("com.joypard.platform.security.resource.ResourceDI");
/**
 * 子资源
 */
K.defView("com.joypard.platform.security.resource.ResourceChildView", {
	resourceView : null,
	resourceChildGridPanel : null,
	resourceChildEditPanel : null,

	/**
	 * 构造函数
	 * 
	 * @param resourceView
	 *            资源视图
	 */
	ResourceChildView : function(resourceView) {
		this.resourceView = resourceView;

		E.addListener("resNodeClick", this.onResNodeClick, this);
		E.addListener("addChildResSuccessEvent", this.onAddChildResSuccess, this);
		E.addListener("delChildResSuccessEvent", this.onDelChildResSuccess, this);
	},

	getResourceChildEditPanel : function() {
		if (this.resourceChildEditPanel == null) {
			this.resourceChildEditPanel = this.createResourceChildEditPanel();
		}
		return this.resourceChildEditPanel;
	},

	getResourceChildGridPanel : function() {
		if (this.resourceChildGridPanel == null) {
			this.resourceChildView = this.createResourceChildGridPanel();
		}
		return this.resourceChildView;
	},

	createResourceChildGridPanel : function() {
		var sm = new K.Ext.grid.CheckboxSelectionModel(), options = {
			title : "子资源",
			iconCls : "p-tab",
			store : ResourceDI.createResChildrensStore(),
			tbar : [ {
				text : "新增",
				iconCls : "add",
				handler : K.createDelegate(this.onAddChildRes, this)
			}, {
				text : "删除",
				iconCls : "delete",
				handler : K.createDelegate(this.onDelChildRes, this)
			} ],
			sm : sm,
			columns : [ sm, {
				header : "资源名称",
				width : 150,
				dataIndex : "name"
			}, {
				header : "类型",
				width : 50,
				dataIndex : "type"
			}, {
				header : "资源编码",
				width : 70,
				dataIndex : "code"
			}, {
				header : "排序号",
				width : 70,
				dataIndex : "sortNo"
			}, {
				header : "二级导航",
				width : 60,
				dataIndex : "menuShowInNav"
			}, {
				id : "resUrl",
				width : 300,
				header : "资源地址",
				dataIndex : "resUrl"
			} ]
		};
		return new Ext.grid.GridPanel(options);
	},

	/**
	 * 创建资源编辑面板
	 */
	createResourceChildEditPanel : function() {
		var options = {
			title : "新增子资源",
			iconCls : "p-edit",
			// showLoadIndicator : true,
			tbar : [ {
				text : "保存",
				iconCls : "save",
				handler : K.createDelegate(this.onSaveResChild, this)
			} ],
			hidden : true
		};
		this.resChildEditPanel = new K.Ext.Panel(options);
		return this.resChildEditPanel;
	},

	/**
	 * 加载子资源信息
	 */
	onResNodeClick : function(resNode) {
		this.curRes = resNode;
		if (this.getResourceChildEditPanel().rendered) {
			this.resourceView.loadResourceChildEditPanel(this.curRes.id);
		}
	},

	/**
	 * 单击新增按钮
	 */
	onAddChildRes : function() {
		this.resourceView.showResourceChildEditPanel(this.curRes.id);
	},
	onDelChildRes : function() {
		var records = this.getResourceChildGridPanel().getSelectionModel().getSelections();
		if (records != null && records.length > 0) {
			if (!confirm("删除资源同时会删除该资源的子资源,您确定要删除吗?")) {
				return;
			}
			E.fireEvent("delChildResEvent", records);
		} else {
			K.err("请选择要删除的子资源", this.getResourceChildGridPanel());
		}
	},

	/**
	 * 保存子资源
	 */
	onSaveResChild : function() {
		var form = $("#resForm", this.resChildEditPanel.getEl().dom), state = form.attr("state"), eventName = null;
		if (state == "update") {
			eventName = "updateChildResEvent";
		} else if (state = "add") {
			eventName = "addChildResEvent";
		}

		if (eventName) {
			E.fireEvent(eventName, form);
		}
	},

	/**
	 * 添加子资源成功
	 */
	onAddChildResSuccess : function(output) {
		var resource = output.getParameter("resource");

		if (this.curRes.id != "0") { // 不是资源根
			this.curRes.appendChild(resource); // 追加树
			if (this.curRes.childNodes.length == 1) {
				this.curRes.expand();
			}
			var newRecord = new (Ext.data.Record.create(ResourceField.fields))(resource, resource.id);
			this.getResourceChildGridPanel().getStore().add(newRecord); // 追加表格
			this.resourceView.showResourceChildEditPanel(this.curRes.id);
		} else {
			resource.text = resource.name;
			resource.leaf = true;
			var newRoot = new Ext.tree.AsyncTreeNode(resource);
			this.resourceView.resTreePanel.setRootNode(newRoot);
			newRoot.select();
		}

		K.m("保存成功").show(this.resChildEditPanel.tbar);
	},

	/**
	 * 删除子资源成功
	 */
	onDelChildResSuccess : function(output) {
		var resIds = output.getParameter("resIds"), store = this.getResourceChildGridPanel().getStore(), selModel = this.getResourceChildGridPanel()
				.getSelectionModel();
		var delRecs = K.Ext.flyStore(store).autoSelectByDelete(selModel, resIds);
		if (delRecs && delRecs.length != 0) {
			K.msg("删除成功", this.getResourceChildGridPanel());
			K.Ext.flyTree(this.resourceView.resTreePanel).removeById(resIds);
		} else {
			K.err("删除失败", this.getResourceChildGridPanel());
		}
	}

// 方法定义
});
